import { getQueryString } from 'nearby-common'
import { BASE_URL } from '@/util'

const curEnv = process.env.NODE_ENV
const isDebug = (function() {
  return curEnv !== 'production'
})()
const isProd = curEnv === 'production'
const isDev = curEnv === 'development'
const isTest = curEnv === 'test'

// 主要接口地址b
const appHost = function() {
  if (isProd) {
    return 'https://nb-oa.lianlianlvyou.com'
  }
  return BASE_URL
}

// 用友系统接口地址
const yyHost = function() {
  if (isProd) {
    return 'https://yongyou.lianlianlvyou.com/fa/yongyou'
  }
  let host = localStorage.getItem('yyHost')
  if (host) return host
  if (isTest) {
    return 'https://yongyou.lianlianlvyou.com/fa/yongyou'
  }
  if (isDev) {
    return 'https://yongyou.lianlianlvyou.com/fa/yongyou'
  }
  return 'https://yongyou.lianlianlvyou.com/fa/yongyou' // 兜底
}
const oaAppHost = function() {
  return appHost()
}

const getToken = function() {
  return getQueryString('token')
}

const getUser = function() {
  return window.JSON.parse(sessionStorage.getItem('user'))
}
const find = function(arr, func) {
  if (arr.constructor !== Array) return undefined
  let len = arr.length
  let res
  for (let i = 0; i < len; i++) {
    if (func(arr[i])) {
      res = arr[i]
      break
    }
  }
  return res
}

export { isDebug, yyHost, getToken, getUser, find, oaAppHost, appHost }
